/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { hvigorTrace } from '../../../../common/trace/hvigor-trace.js';
import { CoreTaskImpl } from '../../../../external/task/core-task-impl.js';
import { HvigorLogger } from '../../../../log/hvigor-log.js';
import { DurationEventState } from '../../../../metrics/event/duration-event.js';
import { MetricLogType } from '../../../../metrics/event/log-event';
import { markTaskExecuteSuccessful } from '../../../../util/task-util.js';
import { formatTime, formatTimeToNumPair, getIntersect } from '../../../../util/time-util.js';
import { TransformHvigorTask } from '../../../../vigor/task/impl/transform-hvigor-task.js';
import { hvigorProcess } from '../../../lifecycle/hvigor-process.js';
import { getModuleNameFromTaskPath } from '../../util/task-path-util.js';
import { taskControlCenter } from '../task-control-center.js';

const log = HvigorLogger.getLogger('hvigor-task-control-center-listener');

/**
 * 任务的单个Work执行失败后触发的事件监听
 *
 * @since 2022/9/1
 */
export const onWorkFailed = (task: CoreTaskImpl, error: Error) => {
  onTaskFailed(task, error);
  hvigorProcess.error(error, task.getTaskLog());
};

/**
 * 整个任务执行结束后触发的事件监听
 *
 * @since 2022/9/1
 */
export const onTaskFinished = (task: CoreTaskImpl) => {
  task.durationEvent.stop(DurationEventState.SUCCESS);
  markTaskExecuteSuccessful(task);
  const taskEndTime: [number, number] = process.hrtime(task.taskExecutedStatus.getTaskBeginTime());
  const totalTime = formatTime(formatTimeToNumPair(getIntersect(task.taskExecutedStatus.getWorkerTimePeriod())));
  const realTime = totalTime === '' ? formatTime(taskEndTime) : totalTime;

  const msg = `Finished :${task.getPath()}`;
  task.durationEvent.setLog(msg, MetricLogType.INFO, task.getDescription(), getIntersect(task.taskExecutedStatus.getWorkerTimePeriod()));
  log._printTaskExecuteInfo(task.getPath(), realTime);

  trace(task, taskEndTime);
  taskControlCenter.setTaskExecuted(task.getPath());
};

function trace(task: CoreTaskImpl, taskEndTime: [number, number]) {
  // 自定义任务不统计
  if (task instanceof TransformHvigorTask) {
    return;
  }

  const moduleName = getModuleNameFromTaskPath(task.getPath());
  hvigorTrace.traceTaskTime(task.getName(), moduleName, taskEndTime[0] * 1e9 + taskEndTime[1]);
}

/**
 * 整个任务被disabled后,执行结束后触发的事件监听
 *
 * @since 2022/9/1
 */
export const onTaskFailed = (task: CoreTaskImpl, error: Error) => {
  task.durationEvent.stop(DurationEventState.FAILED);
  const msg = `Failed :${task.getPath()}`;
  task.durationEvent.setLog(msg, MetricLogType.ERROR, task.getDescription());
  log._printFailedTaskInfo(task.getPath());
  task.onFailed(error);
};

/**
 * 整个任务被disabled后,执行结束后触发的事件监听
 *
 * @since 2022/9/1
 */
export const onTaskDisabled = (task: CoreTaskImpl) => {
  task.durationEvent.stop(DurationEventState.SUCCESS);
  const msg = `Disabled :${task.getPath()}`;
  task.durationEvent.setLog(msg, MetricLogType.INFO, task.getDescription());
  log._printDisabledTaskInfo(task.getPath());
  taskControlCenter.setTaskExecuted(task.getPath());
};

/**
 * 整个任务增量生效Up-to-date,不执行任务时触发的事件监听
 *
 * @since 2022/9/1
 */
export const onTaskUpToDated = (task: CoreTaskImpl) => {
  task.durationEvent.stop(DurationEventState.SUCCESS);
  const msg = `UP-TO-DATE :${task.getPath()}`;
  task.durationEvent.setLog(msg, MetricLogType.INFO, task.getDescription());
  markTaskExecuteSuccessful(task);
  log._printUpToDateTaskInfo(task.getPath());
  taskControlCenter.setTaskExecuted(task.getPath());
};
